package com.google.code.server.http;

import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.Charset;

import com.sun.net.httpserver.Headers;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;

/**
 * get handler which return a static content response as plain text
 * 
 * @author Nadim Benabdenbi
 * 
 * @see HttpHandler
 */
public class StaticTextContentTypePage implements HttpHandler {
	
	private final String content;
	
	private final Charset charset;
	
	private final long responseTime;
	
	public StaticTextContentTypePage() {
		this(Charset.forName("UTF-8"), "<html>\n\t<body>Hello World!</body>\n</html>", -1);
	}
	
	public StaticTextContentTypePage(String content) {
		this(Charset.forName("UTF-8"), content, -1);
	}
	
	public StaticTextContentTypePage(Charset charset, String content, long responseTime) {
		this.content = content;
		this.charset = charset;
		this.responseTime = responseTime;
	}
	
	public void handle(HttpExchange exchange) throws IOException {
		String requestMethod = exchange.getRequestMethod();
		if (requestMethod.equalsIgnoreCase("GET")) {
			if (responseTime > 0) {
				try {
					Thread.sleep(responseTime);
				} catch (InterruptedException e) {
					throw new IOException("failed to sleep", e);
				}
			}
			Headers responseHeaders = exchange.getResponseHeaders();
			responseHeaders.set("Content-Type", "text/plain;charset=" + charset.name());
			exchange.sendResponseHeaders(200, 0);
			OutputStream responseBody = exchange.getResponseBody();
			responseBody.write(content.getBytes(charset));
			responseBody.close();
		}
	}
}